package org.tony.web.spider;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import junit.framework.TestCase;

import org.tony.spider.common.Address;
import org.tony.spider.common.Browser;

public class BrowserDemo extends TestCase
{
  Browser browser;

  @Override
  protected void setUp() throws Exception
  {
    super.setUp();
    browser = new Browser();
    browser.setProxy("10.32.235.41", 8080);
  }

  public void testBrowserGet()
  {
    // browser.open("www.baidu.com", RequestType.get, new Param("aaa", "bbb"));
    System.out.println(new Address("http://www.baidu.com").getParams().size());
  }

  public void testParseParam()
  {
    String http = "http://www.baidu.com";
    String pattern = "(.+)(\\?.+)";
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(http);
    System.out.println(m.find());
    System.out.println(m.group(2));
  }

  public void testGetHost() throws MalformedURLException
  {
    String http = "http://www.qidian.com/BookReader/2358239.aspx";
    URL url = new URL(http);
    System.out.println(url.getHost());
  }

  public void testAddress() throws UnsupportedEncodingException
  {
    Address address = new Address("http://tieba.baidu.com/f");
    address.getParams().put("kw", "圣狱");
    browser.setCharset(Charset.forName("GBK"));
    System.out.println(address.getHost());
    System.out.println(address.buildUrl());

    System.out.println(browser.open(address));
  }

  @Override
  protected void tearDown() throws Exception
  {
    super.tearDown();
  }
}
